<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends BaseController {
    public function admin_list(){
    	//分页
		//每页显示的数据
		$limit=5;
		//查询数据总数
		$count=M('admin')->count();
		//调用TP分页类
		$page=new \Think\Page($count,$limit);
		//生成分页
		$show=$page->show();
		$admin=M('admin')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('show',$show);
		$this->assign('admin',$admin);
		$this->assign('a',$page->firstRow+1);
        $this->display();
    }
    public function admin_add(){
    	if(I('post.')){
            $admin_addtime=!empty($_POST['admin_addtime'])?strtotime($_POST['admin_addtime']):time();
    		$admin_lasttime=!empty($_POST['admin_lasttime'])?strtotime($_POST['admin_lasttime']):time();
    		$data=array(
    			'admin_name'=>I('post.admin_name'),
    			'admin_pwd'=>md5(I('post.admin_pwd')),
                'admin_addtime'=>$admin_addtime,
    			'admin_lasttime'=>$admin_lasttime,
    		);
    		if($_FILES['admin_img']['size']){
    			$upload=new \Think\Upload();
	    		$upload->exts=array('jpg','png','gif');
	    		$upload->rootPath='./Upload/';
	    		$upload->savePath='Admin/';
	    		$info=$upload->upload();
	    		if($info){
	    			$data['admin_img']=$info['admin_img']['savepath'].$info['admin_img']['savename'];
	    		}
    		}
    		$res=M('admin')->add($data);
    		if($res){
    			$this->success('添加成功',U('Admin/admin/admin_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    		}
    	}
    	$this->display();
    }
    public function admin_edit(){
    	$aid=I('get.aid');
    	$admin=M('admin')->where("admin_id=$aid")->find();
    	$this->assign('admin',$admin);
    	if(I('post.')){
    		$data=array(
    			'admin_name'=>I('post.admin_name'),
    			'admin_pwd'=>md5(I('post.admin_pwd')),
    			'admin_addtime'=>strtotime(I('post.admin_addtime')),
    		);
    		$admin_id=I('post.admin_id');
    		if($_FILES['admin_img']['size']){
    			$oldarr=M('admin')->field("admin_img")->where("admin_id=$admin_id")->find();
    			$path='./Upload/'.$oldarr['admin_img'];
    			if(!empty($oldarr) && file_exists($path)){
    				unlink($path);
    			}
    			$upload=new \Think\Upload();
	    		$upload->exts=array('jpg','png','gif');
	    		$upload->rootPath='./Upload/';
	    		$upload->savePath='Admin/';
	    		$info=$upload->upload();
	    		if($info){
	    			$data['admin_img']=$info['admin_img']['savepath'].$info['admin_img']['savename'];
	    		}
    		}
    		$res=M('admin')->where("admin_id=$admin_id")->save($data);
    		if($res){
    			$this->success('编辑成功',U('Admin/admin/admin_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    			exit();
    		}
    	}
    	$this->display();
    }
    public function admin_del(){
    	$a_id=I('get.a_id');
    	$oldarr=M('admin')->field("admin_img")->where("admin_id=$a_id")->find();
		$path='./Upload/'.$oldarr['admin_img'];
		if(!empty($oldarr) && file_exists($path)){
			unlink($path);
		}
		$res=M('admin')->where("admin_id=$a_id")->delete();
		if($res){
			echo 1;exit();
		}else{
			echo M('admin')->getLastSql();exit();
		}
    }
}